import logging
import unittest
from parameterized import parameterized
from api.emp_api import EmpApi
from api.login_api import LoginApi
from common.db_util import DBUtil
from common.read_json_data import build_data
from common.verify_result import common_assert
from config import BODY, BASE_DIR

data = build_data(BASE_DIR + "/data/emp.json")
logging.info(f'data = {data}')
print(f'data = {data}')


class TestEmp(unittest.TestCase):
    mobile = "13344119062"
    au_headers = None

    @classmethod
    def setUpClass(cls) -> None:
        loginapi = LoginApi()
        body = BODY
        cls.au_headers = loginapi.get_au_headers(body)
        print(f"au_headers={cls.au_headers}")
        logging.info(f"au_headers={cls.au_headers}")

        sql = f"delete from bs_user where mobile='{cls.mobile}'"
        n = DBUtil.iud_data(sql)
        print(f"【添加员工前】，删除员工影响的行数为{n}")
        logging.info(f"【添加员工前】，删除员工影响的行数为{n}")

    @classmethod
    def tearDownClass(cls) -> None:
        sql = f"delete from bs_user where mobile='{cls.mobile}'"
        n = DBUtil.iud_data(sql)
        print(f"类后置，删除数据，影响行为{n}")
        logging.info(f"类后置，删除数据，影响行为{n}")

    @parameterized.expand(build_data(BASE_DIR + "/data/emp.json"))
    def test_emp(self, desc, add_emp_body, modify_emp_body, expect_result):
        empapi = EmpApi()
        add_emp_body["mobile"] = self.mobile
        resp = empapi.add_emp(add_emp_body, self.au_headers)
        json_data = resp.json()
        print(f"增加员工的响应结果为{json_data}")
        logging.info(f"增加员工的响应结果为{json_data}")
        common_assert(self, expect_result, resp)
        emp_id = json_data.get("data").get("id")

        resp = empapi.query_emp(emp_id, self.au_headers)
        json_data = resp.json()
        print(f"查询员工的响应结果为{json_data}")
        logging.info(f"查询员工的响应结果为{json_data}")
        common_assert(self, expect_result, resp)

        resp = empapi.query_emp(emp_id, self.au_headers)
        json_data = resp.json()
        print(f"修改员工的响应结果为{json_data}")
        logging.info(f"修改员工的响应结果为{json_data}")
        common_assert(self, expect_result, resp)

        resp = empapi.delete_emp(emp_id, self.au_headers)
        json_data = resp.json()
        print(f"删除员工的响应结果为{json_data}")
        logging.info(f"删除员工的响应结果为{json_data}")
        common_assert(self, expect_result, resp)
